/*
* @Author: syq
* @Date:   2017-12-04 16:34:17
* @Last Modified by:   shenyanqiu
* @Last Modified time: 2017-12-05 16:22:50
*/
import React from 'react';
import PropTypes from 'prop-types';
import { connect } from 'dva';
import styles from './index.less';
import { Page } from 'components';
import queryString from 'query-string';
import { Operation, AddNoticeModal, List } from './components';
import { message } from 'antd';

class WorkNotice extends React.Component {
  static propTypes = {
    loading: PropTypes.object,
    dispatch: PropTypes.func,
    worknotice: PropTypes.object,
    location: PropTypes.object,
  };
  state = {
    deleteArray: [],
  }
  getCheckbox = (data) => {
    console.log(data);
    this.setState({
      deleteArray: data,
    });
  };
  render () {
    const { loading, location, worknotice, dispatch } = this.props;

    location.query = queryString.parse(location.search);
    const { list, pagination } = worknotice;
    const listProps = {
      dataSource: list,
      loading: loading.effects['worknotice/getWorknoticeList'],
      pagination,

      onChange (_pagination) {
        const { query, pathname } = location;
        dispatch({
          type: 'worknotice/getWorknoticeList',
          payload: {
            ...query,
            current: _pagination.current,
            pageSize: _pagination.pageSize,
          },
        });
      },

      onSeeItem (id) {
        dispatch({
          type: 'worknotice/onSeeItemModal',
          payload: {
            id,
          },
        });
      },

      onDeleteItem (id) {
        dispatch({
          type: 'worknotice/onDeleteItemModal',
          payload: {
            params: {
              id,
            }
          },
        });
      },

    };
    // 点击搜索触发请求数据
    const  SearchData = (data) => {
      dispatch({
        type: 'worknotice/searchData',
        payload: {
          formData: data,
        },
      });
    }
    // 提交添加通知
    const doCreateNotice = (values) => {
      console.log(values);
      dispatch({
        type: 'worknotice/doCreateNotice',
        payload: values,
      });
    }
    // 批量删除
    const remove = () => {
      if(this.state.deleteArray.length === 0){
        message.error("请先选择需要删除的信息");
        return;
      }
      dispatch({
        type: 'worknotice/onDeleteItemModal',
        payload: {
          params: {
            ids: this.state.deleteArray,
          },
        }
      });
    }
    return (
      <Page loading={loading.models.worknotice} inner={true}>
        <Operation SearchData={SearchData} remove={remove} />
        <List {...listProps} getCheckbox={this.getCheckbox} />
        <AddNoticeModal doCreateNotice={doCreateNotice} loading={loading.effects['worknotice/doAddNotice']}/>
      </Page>
    );
  }
}

export default connect(({ loading, worknotice }) => ({ loading, worknotice }))(WorkNotice);
